package cn.com.dhc.danlu.activity.delivery.order.viewholder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;

import cn.com.dhc.danlu.activity.delivery.order.decorate.Visitable;

public abstract class BaseViewHolder <T extends Visitable> extends RecyclerView.ViewHolder {
    protected T data;
    private SparseArray<View> views;

    public BaseViewHolder(View itemView) {
        super(itemView);
        views = new SparseArray<>();
    }

    public View getView(int resId) {
        View view = views.get(resId);

        if (view == null) {
            view = itemView.findViewById(resId);
            views.put(resId, view);
        }
        return view;
    }

    public void setData(T data) {
        this.data = data;
        bindViewData(data);
    }

    protected Context getContext() {
        return itemView.getContext();
    }

    /**
     * 绑定数据
     *
     * @param data
     *         数据源
     */
    protected abstract void bindViewData(T data);
}